/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gtracker;

import java.util.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 *
 * @author sv
 */
public class DynInfo {

	public DynInfo() {
		//hashTable = new Map<String, UserInfo>();
		hashTable = new Hashtable();
	}

	public void addUser(String uName, String ipAddr) throws NoSuchAlgorithmException {
		byte[] Sha1NID = Sha1Digest(uName);
		synchronized (this) {
			hashTable.put(uName, new UserInfo(ipAddr, "", Sha1NID));
		}
	}

	public UserInfo searchUName(String uName) {
		return (UserInfo) hashTable.get(uName);
	}

	public boolean isLoggedIn(String uName) {
		return hashTable.containsKey(uName);
	}

	public boolean removeUser(String uName) {

		synchronized (this) {
			return (hashTable.remove(uName) != null);
		}
	}

	static public byte[] Sha1Digest(String msg) throws NoSuchAlgorithmException {
		MessageDigest sha = MessageDigest.getInstance("SHA-1");
		sha.update(msg.getBytes());
		return sha.digest();
	}

	public Vector<String> getUserList() {
		Enumeration<String> enu = hashTable.keys();
		Vector<String> uList = new Vector<String>();
		while (enu.hasMoreElements()) {
			uList.add(enu.nextElement());
		}
		return uList;
	}

	public int getSize() {
		return hashTable.size();
	}

	public UserInfo getUserInfo(String uName) {
		return (UserInfo) hashTable.get(uName);
	}
	protected Hashtable<String, UserInfo> hashTable;
	//protected Hashtable hashTable;
}
